<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-select
          v-model="query.address"
          placeholder="地址"
          class="handle-select mr10"
        >
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select> -->
        <!-- <el-input
          v-model="query.name"
          placeholder="用户名"
          class="handle-input mr10"
        ></el-input> -->
        <!-- <el-button type="primary" @click="handleSearch">搜索</el-button> -->
        <el-button
          type="danger"
          class="handle-del mr10"
          @click="delAllSelection"
          v-if="bimPermissions.includes('ad:banner:delete')"
          >删除</el-button
        >
        <el-button
          type="primary"
          class="handle-del mr10"
          @click="handleIncrease"
          v-if="bimPermissions.includes('ad:banner:save')"
          >添加</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column label="图片" align="center">
          <template slot-scope="scope">
            <el-image
              class="table-td-thumb"
              :src="scope.row.bannerUrl"
              :preview-src-list="[scope.row.bannerUrl]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="bannerName"
          align="center"
          label="标题"
        ></el-table-column>
        <el-table-column
          prop="memo"
          align="center"
          label="备注"
        ></el-table-column>
        <el-table-column
          prop="sort"
          align="center"
          label="排序"
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.$index, scope.row)"
              v-if="bimPermissions.includes('ad:banner:update')"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              v-if="bimPermissions.includes('ad:banner:delete')"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
            <el-button
              type="text"
              v-if="scope.row.enableStatus == 1"
              @click="handleEnable(scope.$index, scope.row)"
              >启用</el-button
            >
            <el-button
              type="text"
              v-if="scope.row.enableStatus == 0"
              @click="handleStop(scope.$index, scope.row)"
              >停用</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import util from "utils/util";
import { mapGetters } from "vuex";
export default {
  name: "bannerimagemanage",
  data() {
    return {
      bimPermissions: [],
      query: {
        // address: "",
        // name: "",
        pageIndex: 1,
        pageSize: 20,
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      pageTotal: 0,
      id: -1,
      ids: [],
      ruleForm: {},
    };
  },
  created() {
    this.getBLMData();
    this.bimPermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      console.log(newVal, oldVal);
      if (oldVal == "/banner-increase") {
        this.bimPermissions = this.permissions;
        return this.getBLMData();
      }
      if (newVal == "/banner-image-manage") {
        this.bimPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      // console.log(newVal, oldVal);
      this.bimPermissions = newVal;
    },
  },
  methods: {
    getBLMData() {
      this.$axios
        .get("/ad/banner/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
          },
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return;
          }
          this.pageTotal = res.data.total || 0;
          this.tableData = res.data.list;
          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
          });
        })
        .catch(() => {});
    },
    // 触发搜索按钮
    // handleSearch() {
    //   this.$set(this.query, "pageIndex", 1);
    //   this.getBLMData();
    // },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.$message.success("删除成功");
          this.tableData.splice(index, 1);
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      var ids = this.multipleSelection.map((item) => {
        return item.id;
      });
      ids = [...new Set(ids)];
      util.delete.call(this, "/ad/banner", "", ids, this.getBLMData);
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids[0] = row.id;
      util.delete.call(this, "/ad/banner", index, this.ids, this.getBLMData);
    },
    // 添加
    handleIncrease() {
      this.$router.push({ path: "/banner-increase" });
    },
    // 编辑操作
    handleEdit(index, row) {
      console.log(index, row);
      this.$router.replace({
        path: "/banner-increase",
        query: { id: row.id },
      });
    },
    // 启用
    handleEnable(index, row) {
      // 二次确认删除
      this.$confirm("确定要启用吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.ruleForm.id = row.id;
          this.ruleForm.bannerName = row.bannerName;
          this.ruleForm.bannerType = row.bannerType;
          this.ruleForm.bannerUrl = row.mobile;
          this.ruleForm.memo = row.memo;
          this.ruleForm.enableStatus = "0";
          this.ruleForm.sort = row.sort;
          this.ruleForm.stuffId = row.stuffId;
          this.saveIncrease();
          // this.getBannerDetail(row.id);
        })
        .catch(() => {});
    },
    // 停用
    handleStop(index, row) {
      console.log(index, row);
      this.$confirm("确定要停用吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.ruleForm.id = row.id;
          this.ruleForm.bannerName = row.bannerName;
          this.ruleForm.bannerType = row.bannerType;
          this.ruleForm.bannerUrl = row.bannerUrl;
          this.ruleForm.memo = row.memo;
          this.ruleForm.enableStatus = "1";
          this.ruleForm.sort = row.sort;
          this.ruleForm.stuffId = row.stuffId;
          this.saveIncrease();
          // this.getBannerDetail(row.id);
        })
        .catch(() => {});
    },
    // getBannerDetail(id) {
    //   this.$axios.get(`/ad/banner/${id}`).then(({ data: res }) => {
    //     console.log(res);
    //     if (res.code != 0) {
    //       return this.$message.error(res.msg);
    //     }
    //     var {
    //       bannerName,
    //       bannerUrl,
    //       stuffId,
    //       sort,
    //       bannerType,
    //       enableStatus,
    //       memo
    //     } = res.data;
    //     this.ruleForm = {
    //       bannerName,
    //       bannerUrl,
    //       stuffId,
    //       sort,
    //       bannerType,
    //       enableStatus,
    //       memo,
    //       id
    //     };
    //     if (this.ruleForm.enableStatus == 1) {
    //       this.ruleForm.enableStatus = 0;
    //     } else if (this.ruleForm.enableStatus == 0) {
    //       this.ruleForm.enableStatus = 1;
    //     }
    //     console.log(this.ruleForm);
    //     this.saveIncrease();
    //   });
    // },
    saveIncrease() {
      // 得到成功的提示
      var ruleForm = this.ruleForm;
      this.$axios.put("/ad/banner", ruleForm).then(({ data: res }) => {
        if (res.code != 0) {
          return;
        }
        this.$message.success("操作成功");
        this.getBLMData();
      });
    },
    // 保存编辑
    saveEdit() {},
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getBLMData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getBLMData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
